package net.dodoman.dynamicproxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class ProxyHandler implements InvocationHandler {

	/**
	 * 绑定,代理类持有被代理类的引用
	 */
	private Object delegate;

	public Object bind(Object delegate) {
		this.delegate = delegate;
		return Proxy.newProxyInstance(delegate.getClass().getClassLoader(),
				delegate.getClass().getInterfaces(), this);
	}
	
	public ProxyHandler(Object delegate){
		this.delegate=delegate;
	}

	@Override
	public Object invoke(Object proxy, Method method, Object[] objs)
			throws Throwable {
		actionBefore();
		Object result = method.invoke(delegate, objs);
		actionAfter();
		return result;
	}

	private void actionBefore() {
		System.out.println("actionBefore");
	}

	private void actionAfter() {
		System.out.println("actionAfter");
	}

}
